package com.wyix.controller;

import com.wyix.common.Result;
import com.wyix.domain.dto.CourseDTO;
import com.wyix.domain.entity.CourseDO;
import com.wyix.service.CourseService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 课程控制器
 */
@RestController
@RequestMapping("/api/courses")
@Tag(name = "课程管理", description = "课程信息的增删改查接口")
public class CourseController {

    @Resource
    private CourseService courseService;

    /**
     * 新增课程
     */
    @PostMapping
    @Operation(summary = "新增课程", description = "添加新的课程信息")
    public Result<?> addCourse(@Valid @RequestBody CourseDTO courseDTO) {
        courseService.addCourse(courseDTO);
        return Result.success();
    }

    /**
     * 根据ID删除课程
     */
    @DeleteMapping("/{id}")
    @Operation(summary = "删除课程", description = "根据ID删除课程信息")
    public Result<?> deleteCourse(
            @Parameter(description = "课程ID", required = true) @PathVariable Long id) {
        courseService.deleteCourse(id);
        return Result.success();
    }

    /**
     * 更新课程信息
     */
    @PutMapping
    @Operation(summary = "更新课程", description = "更新课程信息")
    public Result<?> updateCourse(@Valid @RequestBody CourseDTO courseDTO) {
        courseService.updateCourse(courseDTO);
        return Result.success();
    }

    /**
     * 根据ID查询课程
     */
    @GetMapping("/{id}")
    @Operation(summary = "查询课程", description = "根据ID查询课程详情")
    public Result<CourseDO> getCourseById(
            @Parameter(description = "课程ID", required = true) @PathVariable Long id) {
        CourseDO course = courseService.getCourseById(id);
        return Result.success(course);
    }

    /**
     * 查询所有课程
     */
    @GetMapping("/all")
    @Operation(summary = "查询所有课程", description = "查询所有课程信息（不分页）")
    public Result<List<CourseDO>> getAllCourses() {
        List<CourseDO> courses = courseService.getAllCourses();
        return Result.success(courses);
    }
}
